package com.fr.report.cellElement.core;

import com.fr.base.FRContext;
import com.fr.base.core.list.IntList;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataXMLUtils;
import com.fr.data.util.function.DataFunction;
import com.fr.report.script.Calculator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SummaryGrouper extends RecordGrouper
{
  private DataFunction function;

  public DataFunction getFunction()
  {
    return this.function;
  }

  public void setFunction(DataFunction paramDataFunction)
  {
    this.function = paramDataFunction;
  }

  public Group[] group(TableData paramTableData, int paramInt, int[] paramArrayOfInt, Calculator paramCalculator)
  {
    Object localObject;
    if (this.function == null)
    {
      localObject = new Integer(0);
    }
    else
    {
      this.function.reset();
      if (paramArrayOfInt == null)
        try
        {
          paramArrayOfInt = IntList.range(paramTableData.getRowCount());
        }
        catch (TableDataException localTableDataException1)
        {
          FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
        }
      for (int i = 0; i < paramArrayOfInt.length; ++i)
        try
        {
          if (paramInt == -1)
            this.function.addData(new Integer(paramArrayOfInt[i] + 1));
          else
            this.function.addData(paramTableData.getValueAt(paramArrayOfInt[i], paramInt));
        }
        catch (TableDataException localTableDataException2)
        {
          FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
        }
      localObject = this.function.getResult();
    }
    return ((Group)new Group[] { new Group(localObject, paramArrayOfInt) });
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if ((paramXMLableReader.isChildNode()) && ("FN".equals(paramXMLableReader.getTagName())))
      setFunction(DataXMLUtils.readXMLDataFunction(paramXMLableReader.getElementValue()));
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (this.function != null)
      paramXMLPrintWriter.startTAG("FN").textNode(this.function.getClass().getName()).end();
  }
}